package cn.easier.activity.console.repository;


import cn.easier.activity.web.entity.ActivityInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * 活动信息 Repository
 */
@Repository
public interface ActivityInfoRepository extends JpaRepository<ActivityInfo, Integer>, JpaSpecificationExecutor<ActivityInfo> {

    @Query(value = "SELECT * FROM `activity_info` WHERE id = ? AND create_by = ? AND STATUS <> 2", nativeQuery = true)
    ActivityInfo findOne(Integer id, String user);

    @Query(value = "SELECT * FROM `activity_info` WHERE id = ? AND STATUS <> 2", nativeQuery = true)
    ActivityInfo findByOne(Integer id);

    @Query(value = "UPDATE `activity_info` SET STATUS = ?  WHERE id = ? AND create_by = ?", nativeQuery = true)
    @Modifying
    int delete(Integer status, Integer id, String user);

    @Query(value = "UPDATE `activity_info` SET STATUS = ?  WHERE id = ? ", nativeQuery = true)
    @Modifying
    int deleteSuper(Integer status, Integer id);

    @Query(value = "UPDATE `activity_info` SET STATUS = ? ,foreground_link = ?  WHERE id = ? AND create_by = ?", nativeQuery = true)
    @Modifying
    int release(Integer status,String foregroundLink, Integer id, String user);

    @Query(value = "UPDATE `activity_info` SET release_flag = ? WHERE id = ? AND create_by = ?", nativeQuery = true)
    @Modifying
    int runOrPause(Boolean release, Integer id, String user);

    @Query(value = "UPDATE `activity_info` SET release_flag = ? WHERE id = ? ", nativeQuery = true)
    @Modifying
    int runOrPauseOrSuperUser(Boolean release, Integer id);

    @Query(value = "select template_info_id from activity_info ai group by ai.`template_info_id` order by COUNT(template_info_id) DESC limit ?", nativeQuery = true)
    List<Integer> getHottest(Integer top);

    List<ActivityInfo> findActivityInfosByCreateByAndStatusNotOrderByCreateTimeDesc(String user, Integer status);

    List<ActivityInfo> findByStatusNotOrderByCreateTimeDesc(Integer status);

    @Query(value = "SELECT ai.* FROM activity_info ai LEFT JOIN `activity_prize` ap ON ai.`id` = ap.`activity_info_id` WHERE ap.`id` = ?", nativeQuery = true)
    ActivityInfo findInfo(Integer activityPrizeId);

    @Query(value = "SELECT ai.* FROM activity_info ai LEFT JOIN `activity_prize` ap ON ai.`id` = ap.`activity_info_id` WHERE ai.`create_by` = ? and ap.`id` = ?", nativeQuery = true)
    ActivityInfo findByInfo(String username ,Integer activityPrizeId);

    List<ActivityInfo> findByTypeIdentificationAndCreateByAndStatusNotOrderByCreateTimeDesc(String typeIdentification, String createBy, Integer status);

    List<ActivityInfo> findByTypeIdentificationAndStatusNotOrderByCreateTimeDesc(String typeIdentification,  Integer status);

    Optional<ActivityInfo> findById(Integer id);

    List<ActivityInfo> findByIdIn(Collection<Integer> id);

    List<ActivityInfo> findByTypeIdentificationAndState(String typeIdentification, Integer state);

    @Query("select a from ActivityInfo a where a.createBy = :createBy order by a.createTime asc ")
    List<ActivityInfo> findByCreateByAndCreateTime(@Param("createBy")String createBy);
    
    @Query("select a from ActivityInfo a where a.createBy = :createBy and a.status != :status order by a.id desc ")
    List<ActivityInfo> findByCreateByAndCreateTimeAndStatus(@Param("createBy")String createBy, @Param("status")Integer status);

    @Query("select a from ActivityInfo a where a.status != :status order by a.id desc")
    List<ActivityInfo> findByStatus(@Param("status")Integer status);

    @Query("select a.id from ActivityInfo a where a.status != :status order by a.id desc")
    List<Integer> findByStatus1(@Param("status")Integer status);

    List<ActivityInfo> findByCreateByAndTypeIdentification(String createBy, String typeIdentification);
}
